package cn.wangjun.org.controller;

import cn.wangjun.basic.util.AjaxResult;
import cn.wangjun.basic.util.PageList;
import cn.wangjun.org.domain.Department;
import cn.wangjun.org.query.DepartmentQuery;
import cn.wangjun.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定义接口restful风格
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //添加或修改

    /**
     * restful风格
     * @param department
     * @return
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody Department department){
        System.out.println(department);
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.upadte(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }
    //查询一个
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")//相当于上面这句代码
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.getById(id);
    }
    //查询所有
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.getAll();
    }
    //删除
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    //分页

    /**
     * 页面传的数据
     *     当前页：currentPage
     *     每页条数：pageSize
     * @param departmentQuery
     * @return
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
//        System.out.println("departmentQuery = " + departmentQuery);
        //System.out.println(departmentService.queryData(departmentQuery));
        return departmentService.queryData(departmentQuery);
    }

    /**
     *批量删除
     * 接收的参数   可以使用list，也可以使用数组接收
     * @param ids
     * @return
     */
    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败");
        }
    }

    /**
     * 一对多，下拉框展示上级部门
     * @return
     */
    @GetMapping("/tree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }
}
